package com.runvp.core.config;

import com.runvp.core.interceptor.LoginInterceptor;
import com.runvp.framework.config.properties.IgnoreWhiteProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Slf4j
@Configuration
public class MvcInterceptorConfig implements WebMvcConfigurer {


    // 排除过滤的 uri 地址，nacos自行添加
    @Autowired
    private IgnoreWhiteProperties ignoreWhite;
    @Autowired
    private LoginInterceptor loginInterceptor;
    /**
     * 自定义拦截规则
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> paths = ignoreWhite.getAppPath();
        if(CollectionUtils.isEmpty(paths)){
            log.warn("暂无需要开放的应用级别白名单");
            return;
        }
        for (int i = 0; i < paths.size(); i++) {
            log.info("开放应用级别白名单-{}:【{}】",(i+1),paths.get(i));
        }
        registry.addInterceptor(loginInterceptor).excludePathPatterns(paths).addPathPatterns("/**");
    }
}